package com.test;
import javax.portlet.GenericPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.RenderRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.IOException;
import javax.portlet.PortletRequestDispatcher;

import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.ValidatorException;
import java.net.URLEncoder;

/**
 * Bugzilla Portlet Class
 */
public class Bugzilla extends GenericPortlet {

    public void processAction(ActionRequest request, ActionResponse response) throws PortletException,IOException {
        String error = null;
        String success = null;
        PortletPreferences pref = request.getPreferences();

        if(request.getPortletMode().equals(PortletMode.EDIT)){
            boolean editOk = false;
            String url = request.getParameter("formUrl");
            String login = request.getParameter("formLogin");
            pref.setValue("bugzillaUrl", url);
            pref.setValue("bugzillaLogin", login);
            try{
                if(pref.getValue("bugzillaUrl", "").equals("null")||pref.getValue("bugzillaLogin", "null").equals("")){
                    throw new ValidatorException("Set your Bugzilla Login correctly", null);
                }else{
                    pref.store();
                    editOk = true;
                }
            }catch(ValidatorException ex){
                error = ex.getMessage();
            }
            if(editOk){
                success = "Your request processed successfully.";
                response.setRenderParameter("success", URLEncoder.encode(success, "UTF-8"));
            }else{
                response.setRenderParameter("error", URLEncoder.encode(error, "UTF-8"));
            }
        }
        if(request.getPortletMode().equals(PortletMode.VIEW)){
            String edit = request.getParameter("edit");
            if(edit.equals("link")){
                response.setPortletMode(PortletMode.EDIT);
            }else{
            }
        }

    }
    
    public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {
        response.setContentType("text/html");

        PortletPreferences pref = request.getPreferences();
        String url = pref.getValue("bugzillaUrl", "null");
        String login = pref.getValue("bugzillaLogin", "null");

        request.setAttribute("BUGZILLA_URL", url);
        request.setAttribute("BUGZILLA_LOGIN", login);

        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/Bugzilla_view.jsp");
        dispatcher.include(request, response);
    }
    public void doEdit(RenderRequest request,RenderResponse response) throws PortletException,IOException {
            response.setContentType("text/html");        
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/Bugzilla_edit.jsp");
        dispatcher.include(request, response);
    }
    public void doHelp(RenderRequest request, RenderResponse response) throws PortletException,IOException {

        response.setContentType("text/html");        
        PortletRequestDispatcher dispatcher =
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/Bugzilla_help.jsp");
        dispatcher.include(request, response);
    }
}